package com.midou.administration.controller;


import com.alibaba.fastjson.JSON;
import com.midou.administration.bo.CommonResult;
import com.midou.administration.bo.ShangjiaBo;
import com.midou.administration.entity.Business;
import com.midou.administration.entity.Clerk;
import com.midou.administration.entity.Menu;
import com.midou.administration.service.BusinessService;
import com.midou.administration.service.impl.BusinessServiceImpl;
import com.midou.administration.service.impl.ClerkServiceImpl;
import com.midou.administration.utils.QRCodeUtils;
import org.apache.ibatis.annotations.Param;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author MrChen
 * @since 2021-03-24
 */
@RestController
@RequestMapping("/administration/business")
public class BusinessController {

    //商家 的 实现类
    @Autowired
    private BusinessServiceImpl businessService;

    //商家的接口
    @Autowired
    private BusinessService service;

    @Autowired
    private ClerkServiceImpl clerkService;

    private String staticPath = System.getProperty("user.dir")+"\\midou-restaurant-administration\\src\\main\\resources\\static\\image";

    @RequestMapping("/createShangJia")
    public CommonResult createShangJia(@RequestBody ShangjiaBo shangjiaBo){
        //计算到期时间
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, 1);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date=cal.getTime();
        System.out.println(sdf.format(date));
        //判断是否有相同的账号
        Clerk clerk1=clerkService.selectNumber(shangjiaBo.getClerkAccountNumber());
        if(clerk1!=null){
            return new CommonResult(401,"该账号已经被注册过了,请您换一个");
        }

        //商家注册
        Business business=new Business();
        business.setBuName(shangjiaBo.getBuName());
        business.setBuAddress(shangjiaBo.getBuAddress());
        business.setBuPhone(shangjiaBo.getBuPhone());
        business.setBuDueDate(date);
        business.setBuState(2);
        business.setBuIntroduce("暂无");
        business.setBuRegisterTime(new Date());
        business.setBuBoss(1);
        business.setBuWeChat("暂无");

        Integer num=businessService.createShangJia(business);
        //注册员工
        Clerk clerk=new Clerk();
        clerk.setClerkAccountNumber(shangjiaBo.getClerkAccountNumber());
        clerk.setClerkPassword(shangjiaBo.getClerkPassword());
        clerk.setClerkName(shangjiaBo.getClerkName());
        clerk.setRoleId(1);
        clerk.setClerkAge(shangjiaBo.getClerkAge());
        clerk.setClerkAddress(shangjiaBo.getBuAddress());
        clerk.setClerkPhone(shangjiaBo.getBuPhone());
        clerk.setClerkSex(1);
        clerk.setClerkBusinessId(business.getBuId());


        num+=clerkService.createClerk(clerk);

        if(num==2){
            return new CommonResult(200,"增加成功");
        }else {
            return new CommonResult(500,"增加失败");
        }
    }

    /**
     * 查询商铺
     * @param id
     * @return
     */
    @RequestMapping("/query-shop")
    public Business queryShop(Integer id){
        return businessService.queryShop(id);
    }

    /**
     * 查询所有商家(附带 该商家的特价菜品)
     * @return
     */
    @RequestMapping("/queryBusinessAll")
    public List<Business> queryAll(){
        List<Business> list = this.service.hotMenuByBusiness();
        for (Business business : list) {
            business.setBuLicense(QRCodeUtils.ImageToBase64ByLocal( staticPath+"\\"+ business.getBuLicense()));
                for(Menu menu: business.getMenu()){
                    menu.setMenuImg(QRCodeUtils.ImageToBase64ByLocal(staticPath+"\\"+menu.getMenuImg()));
                }
        }
        return list;
    }
    
    //查询指定商家信息
    @RequestMapping("/businessQueryAll")
    public List<Business> businessQueryAll(@RequestBody Clerk clerk){
        List<Business> list=businessService.businessQueryAll(clerk);
        return list;
    }
    @RequestMapping("/businessQueryAll1")
    public List<Business> businessQueryAll1(@RequestBody Clerk clerk){
        List<Business> list=businessService.businessQueryAll(clerk);
        return list;
    }

    //商家重充值后修改到期时间
    @RequestMapping("/zengjiaTime")
    public CommonResult zengjiaTime(@RequestBody ShangjiaBo shangjiaBo){
        return businessService.zengjiaTime(shangjiaBo);
    }
    //修改商家信息
    @RequestMapping("updateShangjia")
    public CommonResult updateShangjia(@RequestBody Business business){
        return businessService.updateShangjia(business);
    }
    //按照token中的id查询商家名称
    @RequestMapping("doTokenFindShopName")
    public String doTokenFindShopName(@RequestBody Integer id){
        String buName = this.businessService.doTokenFindShopName(id);
        return buName;
    }
    //修改商户头像
    @RequestMapping("/img-upload")
    public Integer imgUpload(@RequestBody Business business) {        return this.businessService.imgUpload(business);
    }
    @RequestMapping("/test")
    public Integer test(){
        return 2;
    }
}
